package com.bai.ruiji.Service.impl;

import com.bai.ruiji.Common.CustomException;
import com.bai.ruiji.Common.R;
import com.bai.ruiji.Service.DishFlavorService;
import com.bai.ruiji.Service.DishService;
import com.bai.ruiji.Service.SetmealDishService;
import com.bai.ruiji.mapper.DishMapper;
import com.bai.ruiji.pojo.Dish;
import com.bai.ruiji.pojo.DishDto;
import com.bai.ruiji.pojo.DishFlavor;
import com.bai.ruiji.pojo.SetmealDish;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 27471
 * Time : 2022/10/10 17:34
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品同时保存口味数据
     *
     * @param dishDto
     */
    @Override
    @Transactional//开启事务
    public void savaWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到dish(菜品)表
        this.save(dishDto);
        //保存菜品口味数据到口味dish_flavor表
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品的信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，dish表
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询菜品对应的口味信息，dish_flavor表
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    @Transactional//事务注解，保证事务一致性(操作两张表)
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品表dish表
        this.updateById(dishDto);
        //清理当前菜品对应的口味数据dish_flavor表的delete操作
            //构造条件
           LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
           queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据dish_flavor的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    @Transactional//事务注解
    @Override
    public void deleteWithFlavor(List<Long> ids) {
        //判断是否启售，并删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("正在售卖中，请停售后再售卖");
        }
        this.removeByIds(ids);

        //删除关联表中的数据菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper1);
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        setmealDishService.remove(dishLambdaQueryWrapper);
    }
}
